package com.jar.exercise.多线程.AQS.中断操作;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;

public class Main {

	public static void main(String[] args) {

		ReentrantLock lock = new ReentrantLock();
		AtomicBoolean locked = new AtomicBoolean(false);
		Runnable runnable = () -> {
			try {
				// 如果抢到锁执行，抢不到
				locked.set(lock.tryLock(3, TimeUnit.SECONDS));
				System.out.println(locked.get());
				test();
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			} finally {
				if (locked.get()) {
					lock.unlock();
				}
			}
		};
		new Thread(runnable).start();
		new Thread(runnable).start();
	}

	public static void test() {
		System.out.println(Thread.currentThread().getName());
	}
}
